package com.dtx.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.dtx.controller.in.EditUserParam;
import com.dtx.controller.in.LoginUserParam;
import com.dtx.controller.in.RegisterUserParam;
import com.dtx.controller.out.UserResult;
import com.dtx.dao.UserDao;
import com.dtx.entity.User;
import com.dtx.module.page.BeePageRequest;
import com.dtx.module.page.BeePageResult;
import com.dtx.msg.ResultMsg;
import com.dtx.service.IUserService;
import org.apache.commons.lang.StringUtils;
import org.beetl.sql.core.page.DefaultPageRequest;
import org.beetl.sql.core.page.DefaultPageResult;
import org.beetl.sql.core.page.PageRequest;
import org.beetl.sql.core.page.PageResult;
import org.beetl.sql.core.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ResultMsg findAll() {
        ResultMsg cacheUser = null;
        try {

            if (redisTemplate.opsForValue().get("user")==null) {
                List<User> user = userDao.createLambdaQuery().andEq(User::getIsDel, 1).select();
                List<UserResult> list = new ArrayList<>();
                UserResult result = null;
                for (User user1 : user) {
                    result = new UserResult();
                    BeanUtil.copyProperties(user1,result);
                    list.add(result);
                }
                redisTemplate.opsForValue().set("user",new ResultMsg(list));
                cacheUser = (ResultMsg) redisTemplate.opsForValue().get("user");
            }else {
                cacheUser =  (ResultMsg) redisTemplate.opsForValue().get("user");
            }

            return cacheUser;

        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail("查询失败");
        }
    }

    @Override
    public ResultMsg findByParam(EditUserParam param) {
        try {
            List<User> user = userDao.createLambdaQuery().andLike(User::getName, Query.filterNull(StringUtils.isBlank(param.getName()) ? null : param.getName()))
                    .andLike(User::getEmail, Query.filterNull(StringUtils.isBlank(param.getEmail()) ? null : param.getEmail()))
                    .andEq(User::getAge, Query.filterNull(param.getAge()))
                    .select();
            List<UserResult> list = new ArrayList<>();
            UserResult result = null;
            for (User user1 : user) {
                result = new UserResult();
                BeanUtil.copyProperties(user1,result);
                list.add(result);
            }
            return new ResultMsg(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail("查询失败");
        }
    }

    @Override
    public ResultMsg register(RegisterUserParam param) {
        User user = null;
        if (param.getEmail()==null && StringUtils.isBlank(param.getEmail())) {
            return ResultMsg.fail("邮箱参数有误");
        }
        if (param.getAge()==null) {
            return ResultMsg.fail("年龄参数有误");
        }
        if (param.getName()==null && StringUtils.isBlank(param.getName())) {
            return ResultMsg.fail("用户名参数有误");
        }
        if (param.getPassword()==null && StringUtils.isBlank(param.getPassword())) {
            return ResultMsg.fail("密码参数有误");
        }


        long count = userDao.createLambdaQuery().andEq(User::getName, param.getName()).andEq(User::getIsDel, 1).count();
        if (count==0) {
            try {
                user = new User();
                BeanUtil.copyProperties(param,user);
                user.setCreateDate(new Date());
                user.setUpdateDate(new Date());
                user.setIsDel(1);
                userDao.createLambdaQuery().insert(user);
                return ResultMsg.success("注册成功");
            } catch (Exception e) {
                e.printStackTrace();
                return ResultMsg.fail("服务器繁忙");
            }
        }else {
            return  ResultMsg.fail("用户名重复了");
        }

    }

    @Override
    public ResultMsg login(LoginUserParam param) {
        User user = userDao.createLambdaQuery().andEq(User::getName, param.getName()).andEq(User::getIsDel, 1).single();
        if (user==null) {
            return ResultMsg.fail("未找到该用户");
        }
        if (!user.getPassword().equals(param.getPassword())) {
            return ResultMsg.fail("用户密码不正确");
        }
        return ResultMsg.success("登陆成功");

        }

    @Override
    public ResultMsg update(EditUserParam param) {
        User user = userDao.createLambdaQuery().andEq(User::getName, param.getName()).andEq(User::getIsDel, 1).single();
        if (user==null) {
            return ResultMsg.fail("未找到该用户");
        }

        return null;
    }

    @Override
    public ResultMsg mdQueryUser(EditUserParam param) {
        Map<String,PageResult> showMap = new HashMap<>();
        param.setPage(param.getPage()==null?1:param.getPage());
        param.setPageSize(param.getPageSize()==null?10:param.getPageSize());
        PageRequest request = DefaultPageRequest.of(param.getPage(), param.getPageSize());
        //BeePageRequest request = BeePageRequest.of(param.getPage(),param.getPageSize());
        DefaultPageResult result = userDao.mdQueryUser2(request);
        //PageResult result1 = userDao.mdQueryUser3(request);
        DefaultPageResult result1 = userDao.mdQueryUser3(request);
        showMap.put("mdQueryUser2",result);
        showMap.put("mdQueryUser3",result1);
        return new ResultMsg(showMap);
    }

}
